package cc.lj.internal.coupons.infrastructure.repository.mysql;

import cc.lj.internal.core.exception.ServiceException;
import cc.lj.internal.core.model.BasePageHelper;
import cc.lj.internal.core.model.PageParam;
import cc.lj.internal.coupons.domain.martcap.repository.ICapMerchantRepository;
import cc.lj.internal.coupons.infrastructure.dao.ICapMerchantMapper;
import cc.lj.internal.coupons.infrastructure.excepion.CoupousExceptionEnum;
import cc.lj.internal.coupons.infrastructure.po.CapBatchPO;
import cc.lj.internal.coupons.infrastructure.po.CapMerchantPO;
import cc.lj.internal.coupons.interfaces.dto.command.CapMerchantClearValueCommand;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository("mysqlCapMerchantRepository")
public class MysqlCapMerchantRepositoryImpl implements ICapMerchantRepository, BasePageHelper<CapMerchantPO> {

    @Autowired
    private ICapMerchantMapper capMerchantMapper;

    /**
     * 倍数模型被修改，当前表的封顶额要被清零
     * @param capMerchantClearValueCommand
     * @return
     */
    @Override
    public Integer updateClearValue(CapMerchantClearValueCommand capMerchantClearValueCommand) {
        QueryWrapper<CapMerchantClearValueCommand> wrapper = new QueryWrapper<>(capMerchantClearValueCommand);
        wrapper.eq("model_id",capMerchantClearValueCommand.getModelId());
        return capMerchantMapper.clearMartCapValue(wrapper);
    }

    @Override
    public Integer getValueSum(CapMerchantPO capMerchantPO) {
        QueryWrapper<CapMerchantPO> wrapper = new QueryWrapper<>(capMerchantPO);
        wrapper.eq("model_id",capMerchantPO.getModelId());
        return capMerchantMapper.martValueSum(wrapper);
    }

    @Override
    public Integer insert(CapMerchantPO entityPO) {
        int rows = capMerchantMapper.insert(entityPO);
        if( rows != 1) {
            throw new ServiceException(CoupousExceptionEnum.INSERT_CAP_MERCHANT_FAILED);
        }
        return entityPO.getId();
    }

    @Override
    public PageInfo<CapMerchantPO> selectPage(PageParam pageParam) {
        return this.page(pageParam);
    }

    @Override
    public Integer deleteById(Integer id) {
        int res = capMerchantMapper.deleteById(id);
        if(res != 1)
        {
            throw new ServiceException(CoupousExceptionEnum.DELETE_CAP_MERCHANT_FAILED);
        }
        return res;
    }

    @Override
    public CapMerchantPO getById(Integer id) {
        CapMerchantPO capMerchantPO = capMerchantMapper.selectById(id);
        if(capMerchantPO == null) {
            throw new ServiceException(CoupousExceptionEnum.CAP_MERCHANT_GETONE_IS_NULL);
        }
        return capMerchantPO;
    }

    @Override
    public Integer updateById(CapMerchantPO capMerchantPO) {
        Integer res = capMerchantMapper.updateById(capMerchantPO);
        if (res != 1)
        {
            throw new ServiceException(CoupousExceptionEnum.UPDATE_CAP_MERCHANT_FAILED);
        }
        return res;
    }

    @Override
    public List<CapMerchantPO> getList(CapMerchantPO capMerchantPO) {
        return this.list(capMerchantPO);
    }

    @Override
    public List<CapMerchantPO> list(CapMerchantPO entityPO) {
        QueryWrapper<CapMerchantPO> wrapper = new QueryWrapper<>(entityPO);
        List<CapMerchantPO> entitys = capMerchantMapper.selectList(wrapper);
        if(entitys == null)
        {
            throw new ServiceException(CoupousExceptionEnum.CAP_MERCHANT_LIST_NULL);
        }
        return entitys;
    }
}
